--- 模块功能：串口收发
-- @author flamebox
-- @license MIT
-- @copyright openLuat
-- @release 2019.2.18
require "utils"
require "pm"
require "config"
require "tool"
require "serviceConfig"
require "nvm"
require "common"

module(..., package.seeall)
TEST_MODE = false
-------------------------------------------- 配置串口 --------------------------------------------

-- 保持系统处于唤醒状态，不会休眠
pm.wake("485_uart")
-- 485芯片相关设置
pmd.ldoset(15, pmd.LDO_VMMC)
local DIR_485 = pins.setup(pio.P0_24, 0)
local cmd_buf
-- 串口ID,串口读缓冲区
local uart_id = 2
-- 串口参数, 固定对应液位仪表
local uart_baud = 2400
local uart_data_bits = 8
local uart_stop_bits = uart.STOP_1
local uart_parity = uart.PAR_NONE
local uart_master = 1 -- 访问的控制器的485地址

local dev_type = nvm.get("DEV_TYPE")
local dev_addr = nvm.get("SLAVE_ID")
local dev_baud = nvm.get("BAUD")

----将1个数字转换成高8位和低8位的2个字节
-- local function intToChar(num)
--    local result = {}
--    result[1] = bit.rshift(num, 8)
--    result[2] = bit.band(num, 0x00ff)
--    return result
-- end

-- modbus 命令返回校验处理
local function checkmodbus(data)
    if string.len(data) < 3 then
        log.warn("MODBUS", "Data is too short")
        return false
    end

    -- 校验读回来的包是否整个crc正确
    if pack.pack('<H', crypto.crc16("MODBUS", string.sub(data, 1, -3))):toHex() ~=
        string.sub(data, -2):toHex() then
        log.warn("MODBUS", "CRC error")
        return false
    end

    return true
end

--[[
函数名：read
功能  ：读取串口接收到的数据
参数  ：无
返回值：无
]]
local function read()
    local data = ""
    cmd_buf = ""
    -- 底层core中，串口收到数据时：
    -- 如果接收缓冲区为空，则会以中断方式通知Lua脚本收到了新数据；
    -- 如果接收缓冲器不为空，则不会通知Lua脚本
    -- 所以Lua脚本中收到中断读串口数据时，每次都要把接收缓冲区中的数据全部读出，这样才能保证底层core中的新数据中断上来，此read函数中的while语句中就保证了这一点
    while true do
        data = uart.read(uart_id, "*l")
        cmd_buf = cmd_buf .. data
        if not data or string.len(data) == 0 then
            break
        end
    end
    -- 打开下面的打印会耗时
    log.info("Uart get ", "msg: " .. string.toHex(cmd_buf))
    if checkmodbus(cmd_buf) == true then
        sys.publish("MY_UART_REC_MODBUS", cmd_buf)
    end
end

--[[
函数名：read
功能  ：读取串口接收到的数据
参数  ：无
返回值：无
]]
local function modbus_read()
    local cacheData = ""
    while true do
        local s = uart.read(uart_id, 1)
        -- local s = uart.read(uart_id, "*l")
        if s == "" then
            if TEST_MODE == false then
                -- if not sys.waitUntil("UART_RECEIVE", 35000 / uart_baud) then
                if not sys.waitUntil("UART_RECEIVE", 35000 / uart_baud) then
                    if cacheData:len() > 0 then
                        log.info("Uart get ", "msg: " .. string.toHex(cacheData))
                        sys.publish("MY_UART_REC_DAT", cacheData)
                        cacheData = ""
                    end
                end
            else
                if cacheData:len() > 0 then
                    log.info("UART_TEST", "get: ", cacheData)
                    if cacheData == "test#" then
                        -- 做为被测试从机时，收到测试命令就要应答
                        uart.write(uart_id, "test_ok")
                    elseif cacheData == "test_ok" then
                        -- 做为测试主机时，收到应答，表示测试ok
                        sys.publish("UART_TEST_OK")
                    end
                    cacheData = ""
                end
                sys.wait(10) -- 这里一定要延时，否则会死机
            end
        else
            cacheData = cacheData .. s
        end
    end
end

if dev_baud == 9999 then
    -- 串口1为rs485
    if dev_type == "TLD" or dev_type == "QH" or dev_type == "TLD2" or dev_type == "FGSG" or dev_type ==
        "9600ZF" or dev_type == "JCIOT" or dev_type == "YFKS" or dev_type ==
        "SHYH" or dev_type == "SHYHC8" or dev_type == "SHYHA9" or dev_type ==
        "DNQ" or dev_type == "TCPW" or dev_type == "QH2" or dev_type == "D2008" or dev_type == "SHYHC602"
        or dev_type == "EI" or dev_type == "ST550" then
        uart_baud = 9600
    elseif dev_type == "4800ZF" or dev_type == "XK3168" or dev_type == "XK3101" or dev_type == "XK3101-D5" then
        uart_baud = 4800
    elseif dev_type == "NM" then
        uart_baud = 2400
    end
else
    uart_baud = dev_baud
end

-- 设置串口
uart.setup(uart_id, uart_baud, uart_data_bits, uart_parity, uart_stop_bits, nil,
    1) -- 最后参数为1 下面的set_rs485_oe才会作用
uart.set_rs485_oe(uart_id, pio.P0_24, 1, 1, 1)

-- uart.on(uart_id, "receive", read)

uart.on(uart_id, "receive", function()
    sys.publish("UART_RECEIVE")
end)

-- modbus_data = string.char(0x01,0x03, 0x00, 0x01, 0x00, 0x0C)
-- 发送modbus 指令
function modbusSend(modbus_data)
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
end

-- 发送测试命令
function sendTest()
    uart.write(uart_id, "test#")
    local result = sys.waitUntil("UART_TEST_OK", 3000)
    if result == false then
        log.info("UART_TEST", "Recv test resp timeout!")
    else
        log.info("UART_TEST", "Recv test ok!")
    end
    return result
end

-- 启动串口数据接收任务
sys.taskInit(modbus_read)

-- 如果读写的设备为南牧仪器
local function handleNMtask()
    -- 南牧仪表
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x01, 0x00, 0x0C)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 01 00 0C")
    end
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- 回复  01 03 18 4字节精度+4字节1号料塔净重+……+4字节5号料塔净重 CRC
    -- 01031800000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF797B
    -- 比如 如果精度是0，整数1234，就是发1234
    -- 01 03 18 00 00 00 00 d2 04 00 00 FF FF FF FF....
    -- 比如 如果精度是1，实际重量是123.4，发送的时候还是发1234
    -- 01 03 18 01 00 00 00 d2 04 00 00 FF FF FF FF....

    if result == true then
        if checkmodbus(cmd_buf) == true then
            local jd = tonumber((string.sub(cmd_buf, 4, 7)):reverse():toHex(),
                16)
            log.info("MODBUS", "NM jingdu: ", jd)      -- 精度
            local val1str = string.sub(cmd_buf, 8, 11) -- 仪表1的值
            log.info("Uart get111 ", "msg: " .. string.toHex(val1str))
            if val1str == string.char(0xff, 0xff, 0xff, 0xff) then
                -- 没有接传感器没有值
                serviceConfig.setWeight(-999999, "无传感器", cmd_buf)
            else
                local val1
                if jd ~= 0 then
                    val1 = tonumber(val1str:reverse():toHex(), 16) / jd / 10 -- 单位kg
                else
                    val1 = tonumber(val1str:reverse():toHex(), 16)           -- 单位kg
                end

                serviceConfig.setWeight(val1, "正常", cmd_buf)
            end
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备IND245为托利多仪器
local function handleTLD_IND245_task()
    -- 托利多仪表,使用出厂默认地址7
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x07, 0x03, 0x00, 0x01, 0x00, 0x02)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 01 00 02")
    end

    -- local modbus_data = string.char(0x07, 0x03, 0x00, 0x01, 0x00, 0x02)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 4)
            local b2 = string.sub(cmd_buf, 5, 5)
            local b3 = string.sub(cmd_buf, 6, 6)
            local b4 = string.sub(cmd_buf, 7, 7)
            local floatstr = b3 .. b4 .. b1 .. b2 -- 3d75c28f
            -- log.info("checkmodbus--",string.toHex(floatstr))
            local val1 = tool.hexToFloat1(floatstr)
            log.info("tld weightval val1--", val1)

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备IND245为托利多仪器
local function handleTLD_IND245_task2()
    sys.taskInit(function()
        -- 托利多仪表,使用出厂默认地址7
        for i = 1, 15 do
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            -- result = true
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local temp = cmd_buf
            if result == true then
                cmd_buf = string.sub(cmd_buf, 2, -1)
                local res1 = tool.Split(cmd_buf, " ")
                local res2 = {}
                -- 分割数据有效才保存判断
                for i, data in ipairs(res1) do
                    if #data > 0 then
                        table.insert(res2, data)
                    end
                end

                for i, data in ipairs(res2) do
                    log.info("res2", i, data)
                end

                log.info("RESULT", res2[1] == "*0", tonumber(res2[2]) ~= nil,
                    tonumber(res2[3]) == 0)
                if #res2 >= 3 and tonumber(res2[2]) ~= nil then
                    local val1 = tonumber(res2[2])
                    log.info("tld weightval val1--", val1)

                    serviceConfig.setWeight(math.floor(val1), "正常", temp) -- 0.07 --> 0
                    break
                else
                    serviceConfig.setWeight(-999999, "校验错误", temp)
                end
            else
                -- 读写失败
                serviceConfig.setWeight(-999996, "无数据返回", temp)
            end

            sys.wait(200)
        end
    end)
end

-- 如果读写的设备为英孚克斯
local function handle_YFKS_task()
    -- ,使用出厂默认地址1
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x00, 0x00, 0x01)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 00 00 01")
    end

    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x00, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 5)
            log.info("b1--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16)
            log.info("英孚克斯 weightval val1--", val1)
            if val1 > 60000 then
                val1 = val1 - 65535
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为上海耀华
local function handle_SHYH_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 02 00 01")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 7)
            log.info("b1--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16)
            log.info("上海耀华 weightval val1--", val1)
            if val1 > 4200000000 then
                val1 = val1 - 4294967295
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为潜合
local function handle_QH_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x02)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 02 00 02")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 7)
            log.info("b1--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16) / 10
            log.info("潜合 weightval val1--", val1)
            if val1 > 4200000000 then
                val1 = val1 - 4294967295
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

local function crc2008(buf)
    log.info("XORSTART")
    local temp = tonumber(string.sub(buf, 1, 2), 16)
    for i = 3, 2, 18 do
        local tmp1 = temp
        local tmp2 = tonumber(string.sub(buf, i, i + 1), 16)
        local str = ""
        repeat
            local s1 = tmp1 % 2
            local s2 = tmp2 % 2
            if s1 == s2 then
                str = "0" .. str
            else
                str = "1" .. str
            end
            tmp1 = math.modf(tmp1 / 2)
            tmp2 = math.modf(tmp2 / 2)
        until (tmp1 == 0 and tmp2 == 0)
        log.info("XOR", tonumber(str, 2))
        temp = tonumber(str, 2)
    end
end

-- 如果读写的设备柯力D2008
local function handle_D2008()
    sys.taskInit(function()
        -- 托利多仪表,使用出厂默认地址7
        for i = 1, 15 do
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            -- result = true
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            -- 022B30303738323530313303
            if result == true then
                local temp = cmd_buf
                cmd_buf = string.toHex(cmd_buf)
                log.info("D2008", #cmd_buf, cmd_buf, string.sub(cmd_buf, 1, 2), string.sub(cmd_buf, -2, -1),
                    string.sub(cmd_buf, 3, -5))
                crc2008(string.sub(cmd_buf, 3, -5))
                if #cmd_buf == 24 and string.sub(cmd_buf, 1, 2) == "02" and string.sub(cmd_buf, -2, -1) == "03" and string.sub(cmd_buf, -2, -1) then
                    log.info("C8", #cmd_buf)
                    cmd_buf = string.sub(cmd_buf, 3, -5)
                    log.info("C8", cmd_buf, string.sub(cmd_buf, 1, 1))
                    local val = tonumber(string.sub(cmd_buf, 4, 4) ..
                        string.sub(cmd_buf, 6, 6) .. string.sub(cmd_buf, 8, 8) .. string.sub(cmd_buf, 10, 10)
                        .. string.sub(cmd_buf, 12, 12) .. string.sub(cmd_buf, 14, 14))
                    local x = tonumber(string.sub(cmd_buf, 16, 16))
                    val = val * 10 ^ x
                    if string.sub(cmd_buf, 1, 2) == "2D" and val ~= nil then
                        val = val * -1
                    end
                    if val ~= nil then
                        serviceConfig.setWeight(math.floor(val), "正常", temp)
                        break
                    else
                        serviceConfig.setWeight(-999999, "校验错误", temp)
                    end
                else
                    serviceConfig.setWeight(-999999, "校验错误", temp)
                end
            else
                -- 读写失败
                serviceConfig.setWeight(-999996, "无数据返回", temp)
            end
            sys.wait(200)
        end
    end)
end

-- 如果读写的设备上海耀华C8
local function handle_YH_C8()
    sys.taskInit(function()
        -- 托利多仪表,使用出厂默认地址7
        for i = 1, 15 do
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            -- result = true
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            -- 022B30303738323530313303
            if result == true then
                local temp = cmd_buf
                cmd_buf = string.toHex(cmd_buf)
                log.info("CMDBUF", string.find(cmd_buf, "022B"))
                log.info("CMDBUF", string.find(cmd_buf, "022D"))
                if string.find(cmd_buf, "022B") ~= nil then
                    local a, b = string.find(cmd_buf, "022B")
                    local hex_str = string.sub(cmd_buf, a, a + 23)
                    log.info("CMDSTR", hex_str)
                    if string.sub(hex_str, 1, 2) == "02" then
                        hex_str = string.sub(hex_str, 3, -5)
                        log.info("NUMSTR",
                            string.sub(hex_str, 4, 4) ..
                            string.sub(hex_str, 6, 6) .. string.sub(hex_str, 8, 8) .. string.sub(hex_str, 10, 10)
                            .. string.sub(hex_str, 12, 12) .. string.sub(hex_str, 14, 14))
                        local val = tonumber(string.sub(hex_str, 4, 4) ..
                            string.sub(hex_str, 6, 6) .. string.sub(hex_str, 8, 8) .. string.sub(hex_str, 10, 10)
                            .. string.sub(hex_str, 12, 12) .. string.sub(hex_str, 14, 14))
                        local x = tonumber(string.sub(hex_str, 16, 16))
                        if val ~= nil and x ~= nil then
                            val = val * 10 ^ x
                            if string.sub(hex_str, 1, 2) == "2D" and val ~= nil then
                                val = val * -1
                            end
                            if val ~= nil then
                                serviceConfig.setWeight(math.floor(val), "正常", temp)
                                break
                            else
                                serviceConfig.setWeight(-999999, "校验错误", temp)
                            end
                        else
                            serviceConfig.setWeight(-999999, "校验错误", temp)
                        end
                    else
                        serviceConfig.setWeight(-999999, "校验错误", temp)
                    end
                end

                if string.find(cmd_buf, "022D") ~= nil then
                    local a, b = string.find(cmd_buf, "022D")
                    local hex_str = string.sub(cmd_buf, a, a + 23)
                    log.info("CMDSTR", hex_str)
                    if string.sub(hex_str, 1, 2) == "02" then
                        hex_str = string.sub(hex_str, 3, -5)
                        local val = tonumber(string.sub(hex_str, 4, 4) ..
                            string.sub(hex_str, 6, 6) .. string.sub(hex_str, 8, 8) .. string.sub(hex_str, 10, 10)
                            .. string.sub(hex_str, 12, 12) .. string.sub(hex_str, 14, 14))
                        local x = tonumber(string.sub(hex_str, 16, 16))
                        if val ~= nil and x ~= nil then
                            val = val * 10 ^ x
                            if string.sub(hex_str, 1, 2) == "2D" and val ~= nil then
                                val = val * -1
                            end
                            if val ~= nil then
                                serviceConfig.setWeight(math.floor(val), "正常", temp)
                                break
                            else
                                serviceConfig.setWeight(-999999, "校验错误", temp)
                            end
                        else
                            serviceConfig.setWeight(-999999, "校验错误", temp)
                        end
                    else
                        serviceConfig.setWeight(-999999, "校验错误", temp)
                    end
                end
            else
                -- 读写失败
                serviceConfig.setWeight(-999996, "无数据返回", temp)
            end
            sys.wait(200)
        end
    end)
end

-- 上海耀华A9
local function handle_YH_A9()
    sys.taskInit(function()
        -- 托利多仪表,使用出厂默认地址7
        for i = 1, 15 do
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            --log.info("A9", cmd_buf, #cmd_buf)
            -- result = true
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            -- 022B30303738323530313303
            if result == true then
                local temp = cmd_buf
                cmd_buf = string.reverse(cmd_buf)
                --log.info("A9", cmd_buf, #cmd_buf)
                if string.sub(cmd_buf, -1, -1) == "=" then
                    local val = 0
                    val = tonumber(string.sub(cmd_buf, 1, -2))
                    log.info("A9", cmd_buf, #cmd_buf, val)
                    if val ~= nil then
                        serviceConfig.setWeight(math.floor(val), "正常", temp)
                        break
                    end
                else
                    serviceConfig.setWeight(-999999, "校验错误", temp)
                end
            else
                -- 读写失败
                serviceConfig.setWeight(-999996, "无数据返回", temp)
            end

            sys.wait(200)
        end
    end)
end

-- 上海耀华A9
local function handle_YH_602()
    sys.taskInit(function()
        -- 托利多仪表,使用出厂默认地址7
        for i = 1, 15 do
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            --log.info("A9", cmd_buf, #cmd_buf)
            -- result = true
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            -- 022B30303738323530313303
            if result == true then
                local temp = cmd_buf
                --cmd_buf = string.reverse(cmd_buf)
                local a, a1 = string.find(cmd_buf, "=")
                local b, b1 = string.find(cmd_buf, "\r")
                log.info("603", a, a1, b, b1)
                if a ~= nil and b ~= nil then
                    local val = 0
                    val = tonumber(string.sub(cmd_buf, a + 1, b - 1))
                    log.info("A9", string.sub(cmd_buf, a + 1, b - 1), cmd_buf, #cmd_buf, val)
                    if val ~= nil then
                        serviceConfig.setWeight(math.floor(val), "正常", temp)
                        break
                    else
                        serviceConfig.setWeight(-999999, "校验错误", temp)
                    end
                else
                    serviceConfig.setWeight(-999999, "校验错误", temp)
                end
            else
                -- 读写失败
                serviceConfig.setWeight(-999996, "无数据返回", temp)
            end

            sys.wait(200)
        end
    end)
end


-- 如果读写的设备为XK3101-D5
local function handle_XK3101D5_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x05, 0x00, 0x02)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 05 00 02")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 5)
            local b2 = string.sub(cmd_buf, 6, 7)
            log.info("SHOW", string.toHex(b2) .. string.toHex(b1))
            local val1 = tonumber(string.toHex(b1) .. string.toHex(b2), 16)
            if val1 > 4290000000 then
                val1 = 4294967295 - val1 + 1
            end
            --log.info("b1--", string.toHex(b1))
            --local val1 = tonumber(b1:toHex(), 16)
            --log.info("XK3101 weightval val1--", val1)
            --if val1 > 60000 then
            --    val1 = 0
            --end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为XK3101
local function handle_XK3101_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x01, 0x00, 0x01)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 02 00 01")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 5)
            log.info("b1--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16)
            log.info("XK3101 weightval val1--", val1)
            if val1 > 60000 then
                val1 = val1 - 65535
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为XK3101
local function handle_XK31682_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x28, 0x00, 0x02)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 28 00 02")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 7)
            log.info("b1--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16)
            log.info("XK3101 weightval val1--", val1)
            if val1 > 4290000000 then
                val1 = 4294967295 - val1 + 1
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为ST550
-- 01 03 04 00 00 42 40 CB 63  （对应48kg）
local function handle_ST550_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x00, 0x00, 0x02)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 00 00 02")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x02, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 5)
            local b2 = string.sub(cmd_buf, 6, 7)
            log.info("b1--", string.toHex(b1))
            log.info("b2--", string.toHex(b2))
            local val1 = tool.hexToFloat(string.toHex(b2 .. b1))
            log.info("ST550 weightval val1--", val1)
            if val1 > 60000 then
                val1 = val1 - 65535
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为狄纳乔
local function handle_DNQ_task()
    uart.write(uart_id, "01READ\r\n")
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        local res = tool.strSplit(cmd_buf, ",")
        log.info("receive", cmd_buf, res[1], res[2])
        if #res >= 3 then
            local val1 = tonumber(res[3])
            log.info("狄纳乔 weightval val1--", val1)
            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备IPE50为法国世感仪器
local function handleTLD_IPE50_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x03, 0x00, 0x01)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 03 00 01")
    end
    -- ,使用出厂默认地址1
    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x03, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 5)
            log.info("b1--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16)
            log.info("fgsg 法国世感 weightval val1--", val1)
            if val1 > 60000 then
                val1 = val1 - 65535
            end

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的是4800或9600的串口不断读写
local function handle4800Or9600ZF_task()
    -- 读取串口发来的数据
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = "ww999.999kg"
    log.info("handle4800Or9600ZF_task--", "1")

    if result == false then
        -- 读写失败
        log.info("handle4800Or9600ZF_task--", "5")
        -- serviceConfig.setWeight(-999996, "无数据返回",cmd_buf)
        return
    end

    log.info("handle4800Or9600ZF_task--", "2")
    local starti, endi = string.find(cmd_buf, "[%d]*%.[%d]*")
    if starti == nil then
        log.info("handle4800Or9600ZF_task--", "4")
        serviceConfig.setWeight(-999999, "正则匹配查找错误", cmd_buf)
        return
    end

    local numstr = string.sub(cmd_buf, starti, endi) -- 999.999
    if numstr ~= nil then
        log.info("handle4800Or9600ZF_task--", "3")
        local num = tonumber(numstr)
        -- num = tonumber(string.format("%.2f", num)) --保留2位小数
        serviceConfig.setWeight(math.floor(num), "正常", cmd_buf) -- 0.07 --> 0
    else

    end
end

-- 柯力XK3168
local xk3168rec = string.char(0x01, 0x00, 0x00, 0x00)
local function handleXK3168_task()
    -- result = true
    -- cmd_buf = FF 11 28 19 04  读数为 41928"
    xk3168rec = ""
    log.info("handleXK3168_task--", "1")
    local maxtry = 0
    local step = 0 -- 0-还没读取到一个ff ，1-- 读取到一个ff  2--读取到第二个ff, 两个ff之间就是要读取的数据
    while true do
        maxtry = maxtry + 1
        -- 连续读取串口发来的数据
        result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
        if result == true and cmd_buf ~= nil then
            cmd_buf = string.sub(cmd_buf, 1, 5)
            log.info("onchar--", string.toHex(cmd_buf))
            if result == false then
                -- 读写失败
                log.info("handleXK3168_task--", "5")
                serviceConfig.setWeight(-999996, "无数据返回", "")
                return
            end
            local recLen = string.len(cmd_buf)
            log.info("handleXK3168_task--", "1")
            for i = 1, recLen do
                local onchar = string.sub(cmd_buf, i, i)
                log.info("onchar--", string.toHex(onchar))
                if onchar == string.char(0xff) then
                    if step == 0 then
                        step = 1
                        xk3168rec = ""
                    elseif step == 1 then
                        if #xk3168rec == 4 then
                            -- 第二次读取到ff
                            log.info("one cmd===", string.toHex(xk3168rec)) -- 11000000
                            -- local aa =  string.char(0x11, 0x28, 0x19, 0x04)
                            local aa = xk3168rec
                            local bb = string.sub(aa, 2)
                            log.info("one cmd12===", string.toHex(bb))
                            local cc = bb:reverse()
                            log.info("one cmd13===", string.toHex(cc))
                            -- bcd码
                            local valstr, _ = string.toHex(cc)
                            local valwe = tonumber(valstr)
                            if valwe ~= nil then
                                serviceConfig.setWeight(valwe, "正常", xk3168rec)
                                return
                            end
                        end
                    end
                else
                    xk3168rec = xk3168rec .. onchar
                end
            end
            if maxtry > 200 then
                -- 避免无限循环
                serviceConfig.setWeight(-999996, "无法解析", xk3168rec)
                return
            end
        end
    end
end

-- 筠城物联网料塔设备
-- 这个设备有点特殊因为发送后luat需要切换为接收模式但该设备在接收到指令后几乎马上返回，但这个时候luat还没切换为接收模式，所以返回数据有一部分没有接收到
-- 解决方法为读取多个寄存器，这样返回的数据就比较长，忽略crc校验，从最后两位往前取数据
local function handleJCIOT_task()
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x04, 0x9B, 0x00, 0x01)
    else
        modbus_data = string.fromHex(dev_addr .. "03 04 9B 00 01")
    end
    --  01 03 04 9B 00 01   返回 0103049B0001F4  要解析为155
    -- local modbus_data = string.char(0x01, 0x03, 0x04, 0x9A, 0x00, 0x05)
    -- local modbus_data = string.char(0x01, 0x03, 0x04, 0x9B, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then

        -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)

        -- local a1 = string.sub(cmd_buf, 4, 4)
        -- local a2 = string.sub(cmd_buf, 5, 5)
        -- local b1 = a2..a1
        if checkmodbus(cmd_buf) == true then
            local b1 = string.sub(cmd_buf, 4, 5)
            log.info("num--", string.toHex(b1))
            local val1 = tonumber(b1:toHex(), 16)
            if val1 > 60000 then
                val1 = val1 - 65535
            end
            log.info("jciot weightval val1--", val1)
            serviceConfig.setWeight(val1, "正常", cmd_buf) -- 0.07 --> 0
        else
            -- 经常出现485modbus 异常去掉异常值
            serviceConfig.setHex(cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

-- 如果读写的设备为TCP-Weight
local function handle_TCPW_task()
    -- ,使用出厂默认地址1
    local modbus_data = ""
    if dev_addr == "FF" then
        modbus_data = string.char(0x01, 0x03, 0x00, 0x04, 0x00, 0x02)
    else
        modbus_data = string.fromHex(dev_addr .. "03 00 04 00 02")
    end

    -- local modbus_data = string.char(0x01, 0x03, 0x00, 0x00, 0x00, 0x01)
    -- 计算CRC
    local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
    local modbus_crc_data = pack.pack('<H', modbus_crc_value)
    log.info("Uart send ",
        "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
    uart.write(uart_id, modbus_data .. modbus_crc_data)
    -- 等待接收 返回 01030209BA3FA7
    result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
    -- result = true
    -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
    if result == true then
        -- if checkmodbus(cmd_buf) == true then
        if checkmodbus(cmd_buf) == true then
            -- cmd_buf = string.char(0x01, 0x03, 0x00, 0xC2, 0x8f, 0x3d,0x75)
            local b1 = string.sub(cmd_buf, 4, 5)
            local b2 = string.sub(cmd_buf, 6, 7)
            local b3 = b2 .. b1
            log.info("b1--", string.toHex(b3))
            local val1 = tonumber(b3:toHex(), 16)
            log.info("英孚克斯 weightval val1--", val1)

            serviceConfig.setWeight(math.floor(val1), "正常", cmd_buf) -- 0.07 --> 0
        else
            serviceConfig.setWeight(-999999, "校验错误", cmd_buf)
        end
    else
        -- 读写失败
        serviceConfig.setWeight(-999996, "无数据返回", cmd_buf)
    end
end

function setWeight(weight)
    dev_type = nvm.get("DEV_TYPE")
    if dev_type == 'JCIOT' then
        local modbus_data = ""
        if dev_addr == "FF" then
            modbus_data = string.fromHex("010600D5" .. string.format("%04X", weight))
        else
            modbus_data = string.fromHex(dev_addr .. "0600D5" .. string.format("%04X", weight))
        end
        local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
        local modbus_crc_data = pack.pack('<H', modbus_crc_value)
        log.info("Uart send ",
            "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
        uart.write(uart_id, modbus_data .. modbus_crc_data)
        -- 等待接收 返回
        result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)

        if result == true then
            local modbus_data = ""
            if dev_addr == "FF" then
                modbus_data = string.fromHex("010601FC0000")
            else
                modbus_data = string.fromHex(dev_addr .. "0601FC000")
            end
            local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
            local modbus_crc_data = pack.pack('<H', modbus_crc_value)
            log.info("Uart send ",
                "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
            uart.write(uart_id, modbus_data .. modbus_crc_data)
            -- 等待接收 返回
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)

            local modbus_data = ""
            if dev_addr == "FF" then
                modbus_data = string.fromHex("010601FC0001")
            else
                modbus_data = string.fromHex(dev_addr .. "0601FC0001")
            end
            local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
            local modbus_crc_data = pack.pack('<H', modbus_crc_value)
            log.info("Uart send ",
                "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
            uart.write(uart_id, modbus_data .. modbus_crc_data)
            -- 等待接收 返回
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            if result == true then
                sys.publish("SET_WEIGHT")
            end
        end
    elseif dev_type == 'YFKS' then
        local res = tool.Split(weight, ",")
        --log.info("SET", #res, weight)
        if #res == 2 then
            --10 00 1D 00 02 04
            local modbus_data = ""
            if dev_addr == "FF" then
                modbus_data = string.fromHex("01 10 00 1D 00 02 04 " ..
                    string.format("%04X", res[1]) .. string.format("%04X", res[2]))
            else
                modbus_data = string.fromHex(dev_addr ..
                    "10 00 1D 00 02 04" .. string.format("%04X", res[1]) .. string.format("%04X", res[2]))
            end
            local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
            local modbus_crc_data = pack.pack('<H', modbus_crc_value)
            log.info("Uart send ",
                "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
            uart.write(uart_id, modbus_data .. modbus_crc_data)
            -- 等待接收 返回
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            if result == true then
                sys.publish("SET_WEIGHT")
            end
        end
    end
end

function setNet()
    dev_type = nvm.get("DEV_TYPE")
    if dev_type == 'JCIOT' then
        local modbus_data = ""
        if dev_addr == "FF" then
            modbus_data = string.fromHex("010601FB0000")
        else
            modbus_data = string.fromHex(dev_addr .. "0601FB0000")
        end
        local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
        local modbus_crc_data = pack.pack('<H', modbus_crc_value)
        log.info("Uart send ",
            "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
        uart.write(uart_id, modbus_data .. modbus_crc_data)
        -- 等待接收 返回
        result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)

        local modbus_data = ""
        if dev_addr == "FF" then
            modbus_data = string.fromHex("010601FB0001")
        else
            modbus_data = string.fromHex(dev_addr .. "0601FB0001")
        end
        local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
        local modbus_crc_data = pack.pack('<H', modbus_crc_value)
        log.info("Uart send ",
            "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
        uart.write(uart_id, modbus_data .. modbus_crc_data)
        -- 等待接收 返回
        result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
        if result == true then
            sys.publish("SET_NET")
        end
    elseif dev_type == 'YFKS' then
        local modbus_data = ""
        if dev_addr == "FF" then
            modbus_data = string.fromHex("01 10 00 30 00 02 04 32 00 32 12")
        else
            modbus_data = string.fromHex(dev_addr .. "10 00 30 00 02 04 32 00 32 12")
        end
        local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
        local modbus_crc_data = pack.pack('<H', modbus_crc_value)
        log.info("Uart send ",
            "msg: " .. string.toHex(modbus_data .. modbus_crc_data))
        uart.write(uart_id, modbus_data .. modbus_crc_data)
        -- 等待接收 返回
        result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
        if result == true then
            sys.publish("SET_NET")
        end
    end
end

local function cmdLed()
    local val = serviceConfig.getData().sensor.weightval
    if val > -9999 then
        local hex = "64 06 00 00 " .. string.format("%04X", val)
        local modbus_data = string.fromHex(hex)
        local modbus_crc_value = crypto.crc16("MODBUS", modbus_data)
        local modbus_crc_data = pack.pack('<H', modbus_crc_value)
        log.info()
        uart.write(uart_id, modbus_data .. modbus_crc_data)
    end
end

----发送命令读取料塔控制器
sys.taskInit(function()
    sys.wait(5000)
    while true do
        if TEST_MODE == false then
            dev_type = nvm.get("DEV_TYPE")
            if dev_type == 'NM' then
                log.info("uart485--task--", "NM")
                handleNMtask()
            elseif dev_type == 'TLD' then
                log.info("uart485--task--", "TLD")
                handleTLD_IND245_task()
            elseif dev_type == 'QH' then
                log.info("uart485--task--", "QH")
                handle_QH_task()
            elseif dev_type == 'XK3101-D5' then
                log.info("uart485--task--", "XK3101D5")
                handle_XK3101D5_task()
            elseif dev_type == 'XK3101' then
                log.info("uart485--task--", "XK3101")
                handle_XK3101_task()
            elseif dev_type == 'TLD2' then
                log.info("uart485--task--", "TLD2")
                handleTLD_IND245_task2()
            elseif dev_type == 'DNQ' then
                log.info("uart485--task--", "DNQ")
                handle_DNQ_task()
            elseif dev_type == 'YFKS' or dev_type == 'EI' then
                log.info("uart485--task--", "YFKS")
                handle_YFKS_task()
            elseif dev_type == 'TCPW' then
                log.info("uart485--task--", "TCPW")
                handle_TCPW_task()
            elseif dev_type == 'SHYH' then
                log.info("uart485--task--", "SHYH")
                handle_SHYH_task()
            elseif dev_type == 'SHYHC8' then
                log.info("uart485--task--", "SHYHC8")
                handle_YH_C8()
            elseif dev_type == 'SHYHA9' then
                log.info("uart485--task--", "SHYHA9")
                handle_YH_A9()
            elseif dev_type == 'FGSG' then
                log.info("uart485--task--", "FGSG法国世感")
                handleTLD_IPE50_task()
            elseif dev_type == 'XK3168' then
                log.info("uart485--task--", "柯力XK3168")
                handleXK3168_task()
            elseif dev_type == 'XK31682' then
                log.info("uart485--task--", "XK316822")
                handle_XK31682_task()
            elseif dev_type == 'JCIOT' then
                log.info("uart485--task--", "筠城物联网")
                handleJCIOT_task()
            elseif dev_type == 'D2008' then
                log.info("uart485--task--", "D2008")
                handle_D2008()
            elseif dev_type == '4800ZF' or dev_type == '9600ZF' then
                log.info("uart485--task--", "4800波特率或9600波特率读写")
                handle4800Or9600ZF_task()
            elseif dev_type == 'SHYHC602' then
                log.info("uart485--task--", "SHYHC602")
                handle_YH_602()
            elseif dev_type == 'ST550' then
                log.info("uart485--task--", "SHYHC602")
                handle_ST550_task()
            end
        end
        if nvm.get("LED") == 1 then
            cmdLed()
        end
        sys.wait(5000)
    end
end)
